package com.zyx.crawlerdemo.utils;

import java.io.*;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 16:02
 * desc:
 */
public class BufferUtils {
    /**
     * 保存缓冲流到文件
     * @param inputStream 输入流
     * @param savePath 保存的文件目录
     * @throws IOException 抛出异常
     */
    public static void bufferToFile(BufferedInputStream inputStream, String savePath) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        // 创建缓冲流
        FileOutputStream fileOutStream = new FileOutputStream(new File(savePath));
        BufferedOutputStream bufferedOut = new BufferedOutputStream(fileOutStream);
        //图片写入
        while ((len = inputStream.read(buffer, 0, 1024)) != -1) {
            bufferedOut.write(buffer, 0, len);
        }
        // 缓冲流释放与关闭
        bufferedOut.flush();
        bufferedOut.close();
    }
}
